package com.xteam.ecsystem.dao.Impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xteam.ecsystem.dao.IFrontSysCfgDao;
import com.xteam.ecsystem.entities.SysNavConfig;
import com.xteam.ecsystem.utils.PageCenterStation;

/**
 * 
 * 前端导航信息实现类
 * @author ls
 *
 */
@Repository
@SuppressWarnings("unchecked")
public class FrontSysCfgDaoImpl extends DDLPrefix implements IFrontSysCfgDao {

	@Autowired
	private BaseDaoImpl baseDao;
	
	public List<SysNavConfig> getNavConfigList(int sncPosition){
		try {
			List<SysNavConfig> list = new ArrayList();
			
			String hql = SQL_FROM + SysNavConfig.REF + SQL_WHERE + " sncState = 1 " 
					+ SQL_AND + "sncPosition = ?"+ SQL_ORDERBY + "sncOrder" + SQL_DESC;
			
			list = this.baseDao.find(hql, new Object[]{sncPosition});
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	public PageCenterStation<SysNavConfig> getNavConfigs(int sncpage, 
			int sncposition, int pageIndex, int pageSize) {
		try {
			List list = new ArrayList();
			String hql = SQL_FROM + SysNavConfig.REF + SQL_WHERE + " sncState = 1 ";
			String countHQL =  "select count(*) " +SQL_FROM + SysNavConfig.REF + SQL_WHERE + " sncState = 1 ";
			if(sncpage != Integer.MAX_VALUE){
				hql += " and sncPage = ?";
				countHQL += " and sncPage = ?";
				list.add(sncpage);
			}
			if(sncposition != Integer.MAX_VALUE){
				hql += " and sncPosition = ?";
				countHQL += " and sncPosition = ?";
				list.add(sncposition);
			}
			PageCenterStation<SysNavConfig> p = baseDao
				.queryList(countHQL, hql, list.toArray(), pageIndex * pageSize, pageSize);
			if(p.getTotal() == 0){
				p = null;
			}
			return p;
		} catch (Exception e) {
			return null;
		}
	}

}
